﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Namespace Infrastructure

    Public Enum ListConstraintType
        Exclude
        Include
    End Enum

    Public Class ListConstraint
        Implements IRouteConstraint
        Public Sub New()
            Me.New(ListConstraintType.Include, New String() {})
        End Sub

        Public Sub New(ByVal ParamArray list() As String)
            Me.New(ListConstraintType.Include, list)
        End Sub

        Public Sub New(ByVal listType As ListConstraintType, ByVal ParamArray list() As String)
            If list Is Nothing Then
                Throw New ArgumentNullException("list")
            End If

            Me.ListType = listType
            Me.List = New List(Of String)(list)
        End Sub

        Private privateListType As ListConstraintType
        Public Property ListType() As ListConstraintType
            Get
                Return privateListType
            End Get
            Private Set(ByVal value As ListConstraintType)
                privateListType = value
            End Set
        End Property

        Private privateList As IList(Of String)
        Public Property List() As IList(Of String)
            Get
                Return privateList
            End Get
            Private Set(ByVal value As IList(Of String))
                privateList = value
            End Set
        End Property

        Public Function Match(ByVal httpContext As HttpContextBase, ByVal route As Route, ByVal parameterName As String, ByVal values As RouteValueDictionary, ByVal routeDirection As RouteDirection) As Boolean Implements IRouteConstraint.Match
            If String.IsNullOrEmpty(parameterName) Then
                Throw New ArgumentNullException("parameterName")
            End If

            If values Is Nothing Then
                Throw New ArgumentNullException("values")
            End If

            Dim value = TryCast(values(parameterName.ToLowerInvariant()), String)
            Dim found = Me.List.Any(Function(s) s.Equals(value, StringComparison.OrdinalIgnoreCase))

            Return If(Me.ListType = ListConstraintType.Include, found, (Not found))
        End Function
    End Class
End Namespace